package com.celesea.workflow.collaboration.v1.controller;

import com.celesea.framework.util.Page;
import com.celesea.framework.util.PageData;
import com.celesea.framework.util.Sort;
import com.celesea.workflow.collaboration.entity.Collaboration;
import com.celesea.workflow.collaboration.service.CollaborationService;
import com.celesea.workflow.collaboration.v1.controller.request.FlowRequest;
import com.celesea.workflow.collaboration.v1.controller.request.PostRequest;
import com.celesea.workflow.collaboration.v1.controller.resource.CollaborationResource;
import com.celesea.workflow.collaboration.vo.FlowVo;
import com.celesea.workflow.core.entity.WorkflowInstance;
import com.celesea.workflow.core.entity.WorkflowNode;
import com.celesea.workflow.core.entity.WorkflowNodeAction;
import com.celesea.workflow.core.entity.WorkflowType;
import com.celesea.workflow.core.service.WorkflowInstanceService;
import com.celesea.workflow.core.service.WorkflowNodeActionService;
import com.celesea.workflow.core.v1.controller.resource.FlowResource;
import com.celesea.workflow.core.vo.WorkflowFilter;
import com.celesea.workflow.core.vo.WorkflowInstanceVo;
import com.celesea.workflow.core.vo.WorkflowNoticeRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author JackYuan
 * @create 2017-11-18 15:42
 */

@RestController
@RequestMapping("v1/collaborations")
@Api(tags = "工作协作单")
public class CollaborationController {

    @Autowired
    private CollaborationService collaborationService;

    @Autowired
    private WorkflowInstanceService workflowInstanceService;

    @Autowired
    private WorkflowNodeActionService workflowNodeActionService;


    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageData<WorkflowInstanceVo> draft(@Valid WorkflowNoticeRequest request, Page page, Sort sort) {
        WorkflowFilter filter = new WorkflowFilter(request, WorkflowType.COLLABORATION.getCode());
        PageData<WorkflowInstance> pageData = workflowInstanceService.selectWorkflow(filter, page, sort);
        return pageData.convert(item -> new WorkflowInstanceVo(item));
    }

    @GetMapping("instance")
    @ApiOperation(value = "构建工作协作单")
    public FlowResource instance() {
        Collaboration model = collaborationService.selectForNew();
        model.applyInfo();
        FlowResource resource = new FlowResource();
        resource.setFormData(new CollaborationResource(model));
        return resource;
    }

    @GetMapping("{instanceId}")
    @ApiOperation(value = "查询工作协作单")
    public FlowResource byInstanceId(@PathVariable String instanceId) {
        WorkflowInstance instance = workflowInstanceService.selectById(instanceId);
        Assert.notNull(instance, "数据不存在");
        Collaboration model = collaborationService.selectById(instance.getWfDataId());
        Assert.notNull(model, "数据不存在");
        WorkflowNode node = new WorkflowNode();
        node.setId(instance.getActiveNodeId());
        List<WorkflowNodeAction> actions = workflowNodeActionService.selectByNode(node);
        FlowResource resource = new FlowResource();
        resource.setActions(actions);
        resource.setFormData(new CollaborationResource(model));
        resource.setInstance(instance);
        return resource;
    }

    @PostMapping
    @ApiOperation(value = "保存工作协作单")
    public CollaborationResource post(@RequestBody @Valid PostRequest request) {
        Collaboration model = collaborationService.createWorkflow(new Collaboration(request));
        return new CollaborationResource(model);
    }

    @PutMapping(value = "flow")
    @ApiOperation(value = "流转工作流")
    public void flow(@RequestBody @Valid FlowRequest request) {
        collaborationService.updateFlow(new FlowVo(request));
    }

}


